var addressArray = [];
$(document).ready(function() {
	queryPrivince();
	listAddress();
	loadPayType();
});	

function loadPayType() {
	$.ajax({
		url:'/pay/listPayType.do',
	    type:'POST', //GET
	    data:{
	    },
	    timeout:5000,    //超时时间
	    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
	    success:function(data){
	    	if(!data.success) {
	    		alert(data.message);
	    		return false;
	    	}
	    	var results = data.data;
	    	var payHtml = '';
	    	for(var i=0;i<results.length;i++) {
	    		var result = results[i];
	    		payHtml += '<li>';
	    		payHtml += '<input type="radio" name="pay_way" value="'+result.payCode+'">';
	    		payHtml += '<label for="weixin_pay">';
	    		payHtml += result.payDesc;
	    		payHtml += '</label>';
	    		payHtml += '</li>';
	    	}
	    	$('#payTypeUL').html(payHtml);
	    	
	    },
	    complete:function(){
	        console.log('结束');
	        addressFlag = true;
	    }
	});
}

function saveAddress() {
	if (addressFlag == false) {
		return false;
	}
	addressFlag = false;
	$.ajax({
		url:'/address/saveAdddress.do',
	    type:'POST', //GET
	    data:{
	    	provinceCode : $('#province').val(),
	    	cityCode : $('#city').val(),
	    	areaCode : $('#area').val(),
	    	detail : $('#user-intro').val(),
	    	realName : $('#user-name').val(),
	    	phone : $('#user-phone').val(),
	    	defaultFlag : $("input:radio[name='defAdd']:checked").val()
	    },
	    timeout:5000,    //超时时间
	    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
	    success : function(data){
	    	if(!data.success) {
	    		alert(data.message);
	    		return false;
	    	}
	    	addressArray.push(data.data);
	    	showAddress();
	    },
	    complete:function(){
	        console.log('结束');
	        addressFlag = true;
	        $("#province").val("0");
    		$('#city').html('<option value="0">--市--</option>');
    		$('#area').html('<option value="0">--区--</option>');
    		$('#user-intro').val('');
    		$('#user-name').val('');
    		$('#user-phone').val('');
    	  	document.getElementById("defAddId").checked = true;
	    }
	});
}

function listAddress() {
	$.ajax({
		url:'/address/listAddress.do',
	    type:'POST', //GET
	    data:{
	    },
	    timeout:5000,    //超时时间
	    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
	    success:function(data){
	    	if(!data.success) {
	    		alert(data.message);
	    		return false;
	    	}
	    	addressArray = data.data;
	    	showAddress();
	    },
	    complete:function(){
	        console.log('结束');
	        addressFlag = true;
	    }
	});
}

function showAddress() {
	var addHtml = '';
	for (var i=0;i<addressArray.length;i++){
		var result = addressArray[i];
		addHtml += '<li>';
		if(1 == result.defaultFlag) {
			$("#default-userName").html(result.realName);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
			$("#default-phone").html(result.phone);
		} else {
			addHtml += '<input type="radio" name="displayAddress" value="'+result.id+'">&nbsp;';
		}
		addHtml += '<span>';
		addHtml += result.realName 
					+ '&nbsp;' 
					+ result.phone 
					+ '&nbsp;' 
					+ result.provinceName 
					+ '&nbsp;' 
					+ result.cityName 
					+ '&nbsp;' 
					+ result.areaName 
					+ '&nbsp;' 
					+ result.detail;
		addHtml += '</span>';
		addHtml += '</li>';
	}
	$('#showAdd').html(addHtml);
}

function provinceChange() {
	var proCode =  $('#province').val();
	if (proCode == 0) {
		$(".city").html('<option value="0">--市--</option>');
		$(".area").html('<option value="0">--区--</option>');
		return false;
	}
	$.ajax({
		url : '/address/listCity.do',
		type : 'POST',
		data : {
			provinceCode : proCode
		},
		timeout : 5000,
		dataType : 'json',
		success : function(data) {
			if(!data.success) {
	    		alert(data.message);
	    		return false;
	    	}
	    	var results = data.data;
	    	$(".city").html('');
	    	for (var i = 0; i < results.length; i++) {
	    		if(i==0) {//默认选中第一条信息
	    			$(".city").append('<option value="'+results[i].city_code+'" selected = "selected">' + results[i].city_name + '</option>');
	    		}
	    		$(".city").append('<option value="'+results[i].city_code+'">' + results[i].city_name + '</option>');
	    	}
	    	cityChange();
		},
		error : function(xhr, textStatus) {
			console.log('错误')
			console.log(xhr)
			console.log(textStatus)
		},
		complete : function() {
			console.log('结束')
		}
	});
}

function queryPrivince() {
	$.ajax({
	 url:'/address/listProvince.do',
	    type:'POST', //GET
	    data:{
	    },
	    timeout:5000,    //超时时间
	    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
	    success:function(data){
	    	if(!data.success) {
	    		alert(data.message);
	    		return false;
	    	}
	    	var results = data.data;
	    	for (var i = 0; i < results.length; i++) {
	    		$(".dosm").append("<option value="+results[i].province_code+">" + results[i].province_name + "</option>")
	    	}
	    	
	    },
		error : function(xhr, textStatus) {
			console.log('错误')
			console.log(xhr)
			console.log(textStatus)
		},
		complete : function() {
			console.log('结束')
			}
		});
	}
	
function cityChange() {
	var cityCode = $('#city').val();
$.ajax({
	url:'/address/listArea.do',
    type:'POST', //GET
    data:{
    	cityCode : cityCode
    },
    timeout:5000,    //超时时间
    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
    success:function(data){
    	if(!data.success) {
    		alert(data.message);
    		return false;
    	}
    	var results = data.data;
    	$(".area").html('');
    	for (var i = 0; i < results.length; i++) {
    		$(".area").append("<option value="+results[i].area_code+">" + results[i].area_name + "</option>")
    	}
    },
	error : function(xhr, textStatus) {
		console.log('错误')
		console.log(xhr)
		console.log(textStatus)
	},
	complete : function() {
		console.log('结束')
		}
	});
}